//
// IBindStatusCallback interface. Support for HTTP POST.

#ifndef LONGKEY_NET_BIND_STATUS_CALLBACK_H_
#define LONGKEY_NET_BIND_STATUS_CALLBACK_H_

#include <Windows.h>
#include <UrlMon.h>
#include <atlbase.h>
#include <atlcom.h>
#include <atlsafe.h>
#include <atlstr.h>
#include "../base/scoped_any.h"
#include "../base/synchronized.h"

class ATL_NO_VTABLE BindStatusCallback 
	: public CComObjectRootEx<CComObjectThreadModel>,
	  public IBindStatusCallback,
	  public IHttpNegotiate
{
public:
	HRESULT Send(BSTR url,
		BSTR post_data,
		BSTR request_headers,
		VARIANT response_headers_needed,
		VARIANT* response_headers,
		DWORD* response_code,
		BSTR* cache_filename);

	HRESULT Cancel();

	// C4505: unreferenced IUnknown local functions have been removed
#pragma warning(disable : 4505)
	BEGIN_COM_MAP(BindStatusCallback)
		COM_INTERFACE_ENTRY(IBindStatusCallback)
		COM_INTERFACE_ENTRY(IHttpNegotiate)
	END_COM_MAP()

	// IBindStatusCallback methods.
	STDMETHODIMP OnStartBinding(DWORD reserved, IBinding* binding);
	STDMETHODIMP GetPriority(LONG* priority);
	STDMETHODIMP OnLowResource(DWORD reserved);
	STDMETHODIMP OnProgress(ULONG, ULONG, ULONG, LPCWSTR);
	STDMETHODIMP OnStopBinding(HRESULT, LPCWSTR);
	STDMETHODIMP GetBindInfo(DWORD* bindf_flags, BINDINFO* bind_info);
	STDMETHODIMP OnDataAvailable(DWORD, DWORD, FORMATETC*, STGMEDIUM*);
	STDMETHODIMP OnObjectAvailable(REFIID, IUnknown*);

	// IHttpNegotiate methods
	STDMETHODIMP BeginningTransaction(LPCWSTR url,
		LPCWSTR request_headers,
		DWORD reserved,
		LPWSTR* additional_headers);
	STDMETHODIMP OnResponse(DWORD response_code,
		LPCWSTR response_headers,
		LPCWSTR request_headers,
		LPWSTR* additional_request_headers);

protected:
	BindStatusCallback();
	virtual ~BindStatusCallback();

private:
	LLock					lock_;
	BINDVERB				http_verb_;
	scoped_hglobal			post_data_;
	DWORD					post_data_byte_count_;
	CString					request_headers_;
	CComSafeArray<DWORD>	response_headers_needed_;
	CComVariant				response_headers_;
	DWORD					response_code_;
	CComGITPtr<IBinding>	binding_git_;
};

#endif	// LONGKEY_NET_BIND_STATUS_CALLBACK_H_